// const { json } = require("body-parser")
const file = require('./FileController.js')

//读取用户列表
const getUserlist = (req, res) => {
    data = file.getItems('user')
    res.json({code:200,data})
}
//新增用户
const createUser = (req, res) => {
    //接受表单数据
    console.log(req.body);
    //添加
    file.createItems(req.body)
    res.json({ code: 201, message: '用户创建成功' });
}

//删除用户
const deleteUser = (req, res) => {
    file.deleteItems(req.body.id)
    res.json({ code: 201, message: '用户删除成功' });
}

//修改用户
const updateUser = (req, res) => {
    const id = req.body.id
    file.updateItems(id,req,res)
    // res.json({ code: 201, message: '用户修改成功' });
}

//查找
const searchUser = (req, res) => {
    // console.log(req.query.id);
    const id = req.query.id
    const user = file.searchItems(id)
    if (user) {
        res.json({ code: 200, message: '用户查询成功', data: user });
    } else {
        res.json({ code: 404, message: '未找到相关用户信息' })
    }
}
module.exports = {
    getUserlist, 
    createUser,
    deleteUser,
    updateUser,
    searchUser,
}